package com.dq.controller.activity;

import com.dq.exception.customException.AdminException;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.setup.Advertising;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/11/23 18:06
 */
@RestController
@RequestMapping("/op/boss/ad")
@Api(description = "广告管理")
public class BossAdController extends AllService {

    @ApiOperation("广告位list")
    @GetMapping("list")
    public Vo adList() throws AdminException {
        List<Advertising> all = adRepository.findByDeletedFalse();
        PageInfo pageInfo = new PageInfo((List<Advertising.Vo>) VO.toVOCollection(all));
        return new Vo(pageInfo);
    }

    @ApiOperation("添加")
    @PostMapping("add")
    public Vo add(@RequestBody Advertising advertising) throws AdminException, IOException {
        adRepository.save(advertising);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("编辑")
    @PostMapping("/edit/{id}")
    public Vo edit(@PathVariable Long id,@RequestBody Advertising advertising) throws AdminException{
        Advertising ad = dao.get(Advertising.class,id);
        if(ad == null){
            throw new AdminException("不存在 请重新选择");
        }
        BeanUtils.copyProperties(advertising,ad, BeanNullUtil.getNullPropertyNames(advertising));
        adRepository.save(ad);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @PostMapping("delete/{id}")
    public Vo delete(@PathVariable Long id) throws AdminException{
        Advertising ad = dao.get(Advertising.class,id);
        if(ad == null){
            throw new AdminException("不存在 请重新选择");
        }
        ad.setDeleted(true);
        adRepository.save(ad);
        return new Vo(HttpStatus.OK);
    }
}
